﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using tools;

public class ZJHDissolutionAlert : MonoBehaviour {

	// Use this for initialization
	private int Time = 30;
	private List<DissolutionCell> list = new List<DissolutionCell>();
	private GameObject Button1;
	private GameObject Button2;
	void Start () {

		for( int i = 2; i < 10 ; i++ ){
			list.Add (transform.GetChild (i).GetComponent<DissolutionCell>());
			transform.GetChild (i).gameObject.SetActive (false);
		}

		Button1 = transform.GetChild (11).gameObject;
		Button2 = transform.GetChild (12).gameObject;
		EventTriggerListener.Get (Button1).onClick = Scuess;
		EventTriggerListener.Get (Button2).onClick = Canel;
	}

	private void init(List<ZjhPlayer> p_list){
		for( int i = 0 ; i < p_list.Count ; i++ ){
			if (p_list [i].Id != 0 ) {
				list [i].gameObject.SetActive (true);
				list [i].Id = p_list [i].Id;
				list [i].P_name = p_list [i].P_name;
				list [i].head = p_list [i].Head.sprite;
			} else {
				list [i].Id = 0;
				list [i].gameObject.SetActive (false);
			}
		}

		Time = 30;
		for( int i = 2; i < 10 ; i++ ){
			transform.GetChild (i).GetComponent<DissolutionCell> ().reset ();
		}
		InvokeRepeating ("ChangeTime", 1f, 1f);
	}

	private void Scuess(GameObject go){
		SoundPlayer.play ("7571");
		HideBtn ();
		GSocket.Send (ByteFactory.makeByte (621103, 1));
	}

	private void Canel(GameObject go){
		SoundPlayer.play ("7571");
		HideBtn ();
		GSocket.Send (ByteFactory.makeByte (621103, 0));
	}

	private void HideBtn(){
		Button1.gameObject.SetActive (false);
		Button2.gameObject.SetActive (false);
	}

	private void ShowBtn(){
		Button1.gameObject.SetActive (true);
		Button2.gameObject.SetActive (true);
	}

	bool flag = false;
	private void ChangeTime(){
		Time--;
		if( Time >= 0 ){
			transform.GetChild (14).GetComponent<Text> ().text = Time.ToString ();
		}

		if (Time < 0) {
			if(!flag){
				flag = true;
				InvokeRepeating ("Close", 3f, 0f);
			}

		}
	}

	public void ChangeTypes(long Id,int types){
		for( int i = 0; i < list.Count ; i++ ){
			if( list[i].Id == Id ){
				list [i].ChangeType (types);
			}
		}

		if(types == 0){
			InvokeRepeating ("Close", 3f, 0f);
		}
	}



	public void Close(){
		CancelInvoke ();
		transform.localPosition = new Vector3(10000,0,0);
		Debug.Log (3333333333333333333);
	}

	public void ShowView(List<ZjhPlayer> p_list,long SendId){
		flag = false;
		init (p_list);
		if (SendId == info.UserInfo.UserData.id) {
			HideBtn ();
		} else {
			ShowBtn ();
		}
		transform.localPosition = Vector3.zero;
	}
	
	// Update is called once per frame
	void Update () {
	
	}
}
